package com.zhenhunfan.io.model.bio.demo03;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * <p>Title: SocketHandler</p>
 * <p>Description: </p>
 *
 * @author zhenhunfan
 * @version 1.0.0
 * @date 2021/3/31
 */
public class SocketHandler implements Runnable {

    private Socket socket;

    public SocketHandler(Socket socket){
        this.socket = socket;
    }

    public void run() {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = socket.getInputStream();
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = in.read(buffer)) > 0) {//阻塞，有请求但没有数据时就一直阻塞在这儿
                System.out.println("input is:" + new String(buffer, 0, length));
                out = socket.getOutputStream();
                out.write("success".getBytes()); //阻塞，当客户端不接收就一直阻塞在这儿
                System.out.println("end");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
